var api = require('../../../config/api.js');
var check = require('../../../utils/check.js');
var app = getApp();
var hostURL = app.globalData1.hostURL;

Page({
  data: {
    // showTopTips: false,
    errorMsg: "",
    pickerHidden: true,
    chosen: ''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

  },

  pickerConfirm: function (e) {
    this.setData({
      pickerHidden: true
    })
    this.setData({
      chosen: e.detail.value
    })
  },
  pickerCancel: function (e) {
    this.setData({
      pickerHidden: true
    })
  },
  pickerShow: function (e) {
    this.setData({
      pickerHidden: false
    })
  },

  formSubmit:function (e) {
    var _this = this
    var nickName = e.detail.value.nickName;
    var phone = e.detail.value.phone;
    var password = e.detail.value.password;
    var subPassword = e.detail.value.subPassword;
    var realname = e.detail.value.realname;

    // console.log(nickName)
    // console.log(phone)
    //判断用户名和密码不得少于3位
    if (password.length < 3 || nickName.length < 3) {
      wx.showModal({
        title: '错误信息',
        content: '用户名和密码不得少于3位',
        showCancel: false
      });
      return false;
    }
    // 判断密码是否为空  
    if ("" == password) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'loading',
        duration: 2000
      })
      return;
    }
    // 两个密码必须一致  
    if (subPassword != password) {
      wx.showToast({
        title: '两次密码不同',
        icon: 'loading',
        duration: 2000
      })
      return;
    }
    // 判断手机号是否为空
    if ("" == phone) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'loading',
        duration: 2000
      })
      return;
    }
    //判断手机号格式是否正确
    //if (!check.isValidPhone(this.data.phone)) {
    //wx.showModal({
    //title: '错误信息',
    //content: '手机号输入不正确',

    //});
    //return ;
    //}

    wx.request({
      url: hostURL+"sharedplatform-web/user/register.action",
      data:
      {
        nickName: nickName,
        phone: phone,
        password: password,
        realname: realname,

      },
      method: 'Post',
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
      },
      success: function (res) {
       
        var errno = res.data.errno
        console.log(errno)
        var userInfo = res.data.userInfo
        if (errno==0) {
          app.globalData2.hasLogin = true;
          wx.setStorageSync('userInfo', res.data.userInfo);
          console.log(userInfo),
          
            wx.setStorage({
              key: "userInfo",
              data: res.data.userInfo,
              success: function () {

                wx.showToast({
                  title: '提示信息',
                  content: '恭喜您注册成功！快去登录吧',
                  showCancel: false
                }),
                  wx.switchTab({
                    url: '/pages/ucenter/index/index'
                  });
              }
            });
        }

        else {
          var errno = res.data.errno;
          if (errno == 2) {
          
            wx.showModal({
              title: '提示信息',
              content: "手机号已被注册！",
              showCancel: false
            })

          }
          else {
            var errno = res.data.errno;
            if (errno == 1){
            wx.showModal({
              title: '错误信息',
              content: '注册失败',
              showCancel: false
            });
            }

          }
        }
      },



  fail: function () {
        console.log("请求失败！")


      },
      complete: function () {
        
      }
    })

  },




  formReset: function (e) {
    console.log('form发生了reset事件，携带数据为：', e.detail.value)
    this.setData({
      chosen: ''
    })
  }
  
})  
